var config = require('../../utils/config');
var util = require('../../utils/util');
Page({ 
    data: {
        userbookId: 0,
        contentId: 0,
        qid: 0,
        isAuth: false,
        src: '',        
        key: '测试关键字',
        page: '1',
        qpage: 0,
        qlist: []
    },
    onLoad(options) {
        let qlist = wx.getStorageSync('qlist');
        const _this = this;
        _this.setData({
            userbookId:options.userbookId,
            contentId:options.contentId,
            qid:options.qid,
            key:options.key,
            page:options.page,            
            qlist:qlist,
            qpage:Number(options.qpage)+1
        })
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.camera']) {
                // 用户已经授权
                _this.setData({
                    isAuth: true
                })
                } else {
                // 用户还没有授权，向用户发起授权请求
                wx.authorize({
                    scope: 'scope.camera',
                    success() { // 用户同意授权
                        _this.setData({
                            isAuth: true
                        })
                    },
                    fail() { // 用户不同意授权
                        _this.openSetting().then(res => {
                            _this.setData({
                                isAuth: true
                            })
                        })
                    }
                })
                }
            },
            fail: res => {
                console.log('获取用户授权信息失败')
            }
        })
    },
    cancelBtn () {
        wx.navigateBack();
        this.setDate({
            isAuth: false
        })
    },
    takePhoto() {
        const _this = this;
        /*wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
            sourceType: ['camera'], //从相册选择
            success: function (res) {
              res.tempFilePaths.forEach((item)=>{
                _this.onUploadFile(item)
              })
            },
        });*/
        const ctx = wx.createCameraContext();
        ctx.takePhoto({
            quality: 'high',
            success: (res) => {
                const tempFilePath = res.tempImagePath;
                _this.setData({
                    src: tempFilePath,
                    isAuth: false
                })
            }
        })
    },
    saveImg() {
        const _this = this;
        _this.onUploadFile(_this.data.src);
    },
    onUploadFile(pathStr){
        let _this = this;
        wx.uploadFile({
            url: config.clubApi.saveWorkPhone,
            method: 'POST',
            filePath: pathStr,
            name: 'file',
            formData: {
                'id': _this.data.userbookId,
                'contentId': _this.data.contentId,
                'qid':_this.data.qid
            },
            success (res){
                var obj = JSON.parse(res.data);
                if (obj.code==="200") {
                    if (obj.msg!='保存成功') {
                        wx.showModal({
                            title: '提示',
                            showCancel: false,
                            content: obj.msg,
                            success: function (res) {}
                        })
                    }else {
                        util.showSuccess('录入成功!', config.showSuccessTime, () => {
                            wx.navigateBack();
                        })
                    }
                }
            }
        })
    }
})